;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.AgentHistoryInventory", [])
        .controller("AgentHistoryInventoryCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService, $stateParams) {
            $scope.handleMultiInput = function(e) {
                $scope.condition.dealerNames = UserContextService.MultiInput(e.target.value);
            };
            
            $scope.markInfo = {
                curRow: -1
            };

            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/report/exportDealerHistoryInventory")
            };

            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
            };
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.condition = {};
            $scope.dataList = [];

            $scope.tbConf = {
                scrollWidth: 1700,
                tableCol: [
                    {
                        name: "代理商名称",
                        dataField: "dealerName",
                        width: 200,
                        isShow: true
                    },
                    {
                        name: "零件编号",
                        dataField: "materialNo",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialName",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "零件分类",
                        dataField: "materialType",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "库存数量",
                        dataField: "amount",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "计量单位",
                        dataField: "unit",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "库存类型",
                        dataField: "stockTypeShow",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "仓库",
                        dataField: "warehouseCode",
                        width: 110,
                        isShow: true
                    },
                    {
                        name: "仓库描述",
                        dataField: "warehouseDes",
                        width: 140,
                        isShow: true
                    },
                    {
                        name: "库区",
                        dataField: "areaCode",
                        width: 110,
                        isShow: true
                    },
                    {
                        name: "储位",
                        dataField: "locationCode",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "进货基准价",
                        dataField: "price",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "备份日期",
                        dataField: "backupDateShow",
                        width: 140,
                        isShow: true
                    },
                ]
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index;
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.handleDate = function(val) {
                $scope.condition.backupDate = $filter("YbDate")(val, 'yyyy-MM');
            };

            $scope.beforeExport = function() {
                if (!$scope.condition.dealerName && !$scope.condition.dealerNames) {
                    ybWidget.ShowMessage("请输入代理商名称", MESG_TYPE.Error);
                    return false;
                }
                if (!$scope.condition.backupDate) {
                    ybWidget.ShowMessage("请选择备份日期", MESG_TYPE.Error);
                    return false;
                }
                return true;
            };

            function getDateWithLastDay(dateStr) {
                const date = new Date(dateStr);
                const year = date.getFullYear();
                const month = date.getMonth() + 1;
                const lastDay = new Date(year, month, 0).getDate();
                return `${year}-${month}-${lastDay}`;
            }
            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }

                var service = "device";
                var reqUrl = "/report/dealerHistoryInventory";
                var param = {
                    pageNum: $scope.pageInfo.curPage,
                    pageSize: $scope.pageInfo.pageSize,
                    filters: angular.copy($scope.condition)
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }

                    const resT = resp.value.list || []
                    resT.forEach(item => {
                        item.stockTypeShow = $filter('storageType')(item.stockType);
                        item.backupDateShow = getDateWithLastDay(item.backupDate);
                    })
                    $scope.dataList = resT;
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                });
            }

            (function () {
                getDataList();
            })();
        }])
    ;
})(window, window.angular);